package com.tianzj.mvc.system.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.mvc.system.pojo.SysUser;
import com.tianzj.mvc.system.service.SysUserService;
import com.tianzj.util.AjaxJson;


@RestController
public class LoginController  {

	private Logger logger  =  LoggerFactory.getLogger(LoginController.class );

	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("/index")
	public ModelAndView index(){
		return new ModelAndView("login");
	}
	
	@RequestMapping("/loginIn")
	public AjaxJson index(@RequestParam(value="userName")String userName,@RequestParam(value="password")String password){
	    UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            SysUser sysUser=(SysUser) subject.getPrincipal();
            Session session = SecurityUtils.getSubject().getSession();
            session.setAttribute("sessionUser",sysUser);
            //重定向到showIndex
           return AjaxJson.success();
        }catch(UnknownAccountException uae) {
        	logger.info("用户名为【" + token.getPrincipal() + "】不存在");
		} catch (IncorrectCredentialsException ice) {
			logger.info("用户名为【 " + token.getPrincipal() + " 】密码错误！");
		} catch (LockedAccountException lae) {
			logger.info("用户名为【" + token.getPrincipal() + " 】的账户锁定，请联系管理员。");
		} catch (DisabledAccountException dax) {
			logger.info("用户名为:【" + token.getHost() + "】用户已经被禁用.");
		} catch (ExcessiveAttemptsException eae) {
			logger.info("用户名为:【" + token.getHost() + "】的用户登录次数过多，有暴力破解的嫌疑.");
		} catch (ExpiredCredentialsException eca) {
			logger.info("用户名为:【" + token.getHost() + "】用户凭证过期.");
		} catch (AuthenticationException ae) {
			logger.info("用户名为:【" + token.getHost() + "】用户验证失败.");
		} catch (Exception e) {
			logger.info("别的异常信息。。。。具体查看继承关系");
		}
        return AjaxJson.error("登录失败");
	}
	
	@RequestMapping("/main")
	public ModelAndView main(){
		return new ModelAndView("main");
	}
	
	@RequestMapping("/showIndex")
	public ModelAndView showIndex(){
		return new ModelAndView("index");
	}
	@RequestMapping("/showIndex1")
	public ModelAndView showIndex1(){
		return new ModelAndView("dashboard");
	}
	
	@RequestMapping("/logOut")
	public ModelAndView logOut(){
		SecurityUtils.getSubject().logout();
		return new ModelAndView("redirect:/index");
	}
}
